<?php
/**
 * Created by PhpStorm.
 * User: 73640
 * Date: 2018/10/18
 * Time: 15:44
 */

namespace App\Repository;


use App\model\Park;

class ParkRepository
{
    /**
     * 园区列表
     *
     * @param  $request
     * city_id 城市
     * city_first_id 二级城市id
     * city_second_id 三级
     * area_id
     * sreach_key
     * 筛选条件
     * page_id 页码
     * 排序条件
     * sort_type  字段
     * 排序字段
     * sort 升序 降序
     *
     *link_model industry,propertyType,park
     */
    public function getList($request){
         $park=Park::with(['parkType','parkFormat']);
         if(isset($request['city_id'])&&$request['city_id']!=0){
             $park=$park->where('park.city_id',$request['city_id']);
         }
       /* if(isset($request['park_area_id'])&&$request['park_area_id']!=0){
            $park=$park->where('park.park_area_id',$request['park_area_id']);
        }*/
         if(isset($request['area_id'])&&$request['area_id']!=0){
             $park=$park->where('park.area_id',$request['area_id']);
         }
        if(isset($request['area1_id'])&&$request['area1_id']!=0){
            $park=$park->where('park.area1_id',$request['area1_id']);
        }
        if(isset($request['is_hot'])&&$request['is_hot']!=0){
            $park=$park->where('park.is_hot',$request['is_hot']);
        }
        if(isset($request['park_area_id'])&&$request['park_area_id']!=0){
            $park=$park->where('park.park_area_id',$request['park_area_id']);
        }
        if(isset($request['search_key'])&&$request['search_key']!=''){
            $park=$park->where('park.title','like','%'.$request['search_key'].'%');
        }
        if(isset($request['sort_type'])&&$request['sort_type']!=''){
            $park=$park->orderBy('park.'.$request['sort_type'],$request['sort'] ?? 'asc');
        }else{
            $park=$park->orderBy('park.id','desc');
        }


       $park=$park->select('park.*');
        $pagesize=$request['pagesize'] ?? 10;
       $park= $park->paginate($pagesize);
       return $park;
    }

    /**
     * @param int $workshop_id
     */
    public function detail(int $park_id){
        $park=Park::with(['parkType','parkFormat','workshop'=>function($query){
            $query->with('propertyType')->take(4);
        }])->where('id',$park_id)->first()->toArray();

        return $park;
    }
    public function area(){

    }

}